<template>
  <div class="tableRadio">
    <h3>table表格跨页单选</h3>
    <br />
    <el-table
      ref="table"
      :data="list"
      tooltip-effect="dark"
      highlight-current-row
      style="width: 100%"
      @current-change="selectCurrentChange"
    >
      <el-table-column align="center" width="100">
        <template slot-scope="scope">
          <el-radio v-model="selectRow" :label="scope.row.id">&nbsp;</el-radio>
        </template>
      </el-table-column>
      <el-table-column label="日期">
        <template slot-scope="scope">{{ scope.row.date }}</template>
      </el-table-column>
      <el-table-column prop="name" label="姓名"> </el-table-column>
    </el-table>
    <el-pagination
      class="pagination"
      @current-change="handleCurrentChange"
      :current-page.sync="pageNo"
      background
      :page-size="pageSize"
      layout="total,prev, pager, next, jumper"
      :total="tableData.length"
    />
  </div>
</template>
<script>
export default {
  name: 'tableRadio',
  data() {
    return {
      list: [],
      pageNo: 1,
      pageSize: 10,
      selectRow: null, // 选中的行
    }
  },
  computed: {
    tableData() {
      return Array.from({ length: 1000 }, (_, index) => {
        return {
          date: '2024-3-4',
          id: index,
          name: '李小明' + index,
        }
      })
    },
  },
  mounted() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.list = this.tableData.slice(
        (this.pageNo - 1) * this.pageSize,
        this.pageNo * this.pageSize
      )
      if (this.selectRow) {
        const row = this.list.find((item) => item.id === this.selectRow)
        if (row) {
          this.$nextTick(() => {
            this.$refs.table.setCurrentRow(row)
          })
        }
      }
    },
    handleSelectionChange(val) {
      if (val.length > 1) {
        this.$refs.table.clearSelection()
        this.$refs.table.toggleRowSelection(val.pop())
      }
    },
    handleCurrentChange(val) {
      this.pageNo = val
      this.fetchData()
    },
    selectCurrentChange(row) {
      this.selectRow = row.id
    },
  },
}
</script>
<style lang="less" scoped>
.pagination {
  text-align: center;
  margin-top: 20px;
}
</style>
